package com.smallchill.common.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.beetl.sql.core.ConnectionSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.smallchill.core.utils.Prop;

public class DataBaseConnection implements ConnectionSource {

	private static DruidDataSource dataSource = new DruidDataSource();

	private static String driverName;
	private static String url;
	private static String user;
	private static String password;

	static {
		Prop prop = Prop.init("config.properties");
		driverName = prop.get("master.driver");
		url = prop.get("master.url");
		user = prop.get("master.username");
		password = prop.get("master.password");
		dataSource.setDriverClassName(driverName);
		dataSource.setUrl(url);// 连接地址
		dataSource.setUsername(user);// 用户名
		dataSource.setPassword(password);// 密码
		dataSource.setInitialSize(2);
		dataSource.setMaxActive(20);
		dataSource.setMinIdle(0);
		dataSource.setMaxWait(60000);
		dataSource.setValidationQuery("SELECT 'x' from dual");
		dataSource.setTestOnBorrow(false);
		dataSource.setTestWhileIdle(true);
		dataSource.setPoolPreparedStatements(false);
	}

	private Connection _getConn() {
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	@Override
	public Connection getMaster() {
		return _getConn();
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Connection getConn(String sqlId, boolean isUpdate, String sql,
			List paras) {
		return _getConn();
	}

	@Override
	public boolean isTransaction() {

		return false;
	}

	@Override
	public Connection getSlave() {
		return this.getMaster();
	}

	@Override
	public void forceBegin(boolean isMaster) {

	}

	@Override
	public void forceEnd() {

	}

}
